<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mindex extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function sendmail()
    {
        
        $config = array(
                        'protocol' => 'smtp',
                        'smtp_host' => 'mail.nhactinhca.com',
                        'smtp_port' => '25',
                        'smtp_user' => 'noreply@nhactinhca.com',
                        'smtp_pass' => 'l))FbSbIg^]5',
                        'mailtype' => 'html'
                        );
        $this->load->library('email',$config);
        $body = $_POST['content'];
        $list = $this->getParamInt('list');
        $title = $this->getParamString('title');
        $datas = $this->db->query('SELECT * FROM email_receive WHERE list_s = '.$list)->result_object();
       
       foreach($datas AS $el=>$le)
       {
            
            $this->email->from('noreply@nhactinhca.com', $title);
            $this->email->to($le->email);    
            $this->email->subject($title);
            $this->email->message($body);    
            
            if($this->email->send()){
                   
                    return 200;
                }
     
        }
        
    }
    
    public function getSelectList()
    {
        $data = $this->db->query('SELECT * FROM email_receive GROUP BY list_s ORDER BY list_s ASC')->result_object();
        $html  = '';
        foreach($data AS $el=>$le)
        {
            $html .= '<option value="'.$le->list_s.'">'.$le->list_s.'</option>';
        }
        return $html;
    }
    
    public function getEmailDB($number,$offset)
    {
        $id = $this->getParamUri(3,2);
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT * FROM email_receive WHERE 1=1 ";
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND email like '%".$key."%' ";
        }
        $sql .= "ORDER BY id DESC";
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
        
    }
    public function deleteemail()
    {
        $id = $this->getParamInt('id');
        $this->db->delete('email_receive',"id = '".$id."'");
        return 200;
    }
    
}